package com.deyou.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;

import com.deyou.Constants;
import com.deyou.R;
import com.deyou.model.HouseResourceDetailBean;
import com.deyou.util.DateUtil;
import com.squareup.timessquare.CalendarCellDecorator;
import com.squareup.timessquare.CalendarCellView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yangyuancheng on 2017/7/11.
 */

public class DateRentStatusDecorator implements CalendarCellDecorator {

    private Context context;

    //加入这个typeMark，是因为我的需求是如果涉及到酒店的预订就需要在日期范围选择的时候开始和结束时间显示
    //“入住和离店“，而对于预订机票的时候又需要显示“去程和返程“的个性化需求。
    // typeMark 预留
    private String typeMark;

    private List<HouseResourceDetailBean.HouseresourcePriceBean> priceBeanList = new ArrayList<>();

    public DateRentStatusDecorator(Context context, List<HouseResourceDetailBean.HouseresourcePriceBean> priceList) {
        this.priceBeanList = priceList;
        this.context = context;
    }

    public DateRentStatusDecorator(Context context, String typeMark) {
        this.context = context;
        this.typeMark = typeMark;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void decorate(CalendarCellView cellView, Date date) {
        //我用了一个讨巧的方法，可能略显笨拙。
        //利用更换背景的方式免于具体操作单个日期块的样式，因为每天的日期块修改的话会同时修改，利用更换背景加上
        //判断返回的tag字符串标识可以暂时绕过这个不好处理的每个日期块都会影响的问题。
        Drawable beginDraw = context.getResources().getDrawable(R.drawable.calender_bg_selector, null);
        Drawable endDraw = context.getResources().getDrawable(R.drawable.calender_bg_selector, null);
        Drawable middleDraw = context.getResources().getDrawable(R.color.color_FFAA73, null);
        Drawable combineDraw = context.getResources().getDrawable(R.color.color_FFAA73, null);
        Drawable todayDraw = context.getResources().getDrawable(R.color.color_272424, null);
        Drawable blankDraw = context.getResources().getDrawable(R.color.white, null);
        String dateString = DateUtil.date2String10(date);

        String showDayValue = "";
        if (cellView.isToday())
            showDayValue = "今天";
        else
            showDayValue = Integer.toString(date.getDate());

        if (cellView.isSelectable()) { //先通过是否可选方法来区分时间,如果可选则再判断是否已选
            if (cellView.isSelected()) {//再通过是否已选来设置日期的背景图片
                SpannableString string = new SpannableString(showDayValue + "\n已租");
                string.setSpan(new RelativeSizeSpan(0.7f), showDayValue.length(), string.length(),
                        Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                cellView.setBackground(middleDraw);
                cellView.getDayOfMonthTextView().setText(string);
                cellView.getDayOfMonthTextView().setTextColor(context.getResources().getColor(R.color.white));
            } else {
                cellView.setBackground(blankDraw);
                String price = "";
                if (null != priceBeanList && priceBeanList.size() > 0) {
                    for (HouseResourceDetailBean.HouseresourcePriceBean priceBean : priceBeanList) {
                        if (priceBean.priceDate.equals(dateString)) {
                            price = "\n" + Constants.RMB_UNIT + "" + priceBean.price;
                            break;
                        }
                    }
                } else {
                    price = "";
                }
                SpannableString string = new SpannableString(showDayValue + price);
                string.setSpan(new RelativeSizeSpan(0.7f), showDayValue.length(), string.length(),
                        Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                cellView.getDayOfMonthTextView().setText(string);
                cellView.getDayOfMonthTextView().setTextColor(context.getResources().getColor(R.color.color_272424));
            }
        } else {//如果为不可选时间则直接设置日期背景
            String json = String.valueOf(cellView.getTag());
            String currentMonthBoolean = json.substring(json.indexOf("isCurrentMonth") + 15, json.indexOf("isCurrentMonth") + 20);
            if ("false".equals(currentMonthBoolean)) {
                cellView.getDayOfMonthTextView().setText("");
                cellView.setBackground(blankDraw);
            } else {
                cellView.setBackground(blankDraw);
//                cellView.setBackground(context.getResources().getDrawable(R.color.color_F3F2F2, null));
                cellView.getDayOfMonthTextView().setTextColor(context.getResources().getColor(R.color.color_959090));
            }
        }
    }
}
